package com.snake.scoreboardlib;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;


public class ScoreboardList {
		private List<ScoreboardItem> list;
		private Context  context;

		//constructor
		public ScoreboardList(Context context)
		{
			this.context = context;
			list = new ArrayList<ScoreboardItem>();
		
		} 
		
	public ScoreboardItem Add() {
		ScoreboardItem item = new ScoreboardItem();
		
		return Add(item);
		
	}
	public Boolean Del(ScoreboardItem item)
	{
		return list.remove(item);
	}
	public ScoreboardItem Add(ScoreboardItem item)
	{
		list.add(item);
     	item.setDUID(GetDUID());
		
		return item;
		
	}
	public int GetCount() {
		return list.size();
	}
	public ScoreboardItem GetItem(int index)
	{
		if (index < list.size())
			return list.get(index);
		else
			return null;
	}
	private String GetDUID()
	{
		return OSHelper.GetDUID(context);
	}
	
	public String toJSON() throws JSONException
	{
		JSONArray jolist = new JSONArray();
		for(ScoreboardItem item: list)
		{
			
			jolist.put(item.getJSONObject());
			
			
		}
		
		return jolist.toString();
	}
	public void fromJSON(String json) throws JSONException
	{
		JSONArray jolist = new JSONArray(json);
		for(int i=0; i<jolist.length(); i++)
		{
			JSONObject jo = jolist.getJSONObject(i);
			ScoreboardItem item = new ScoreboardItem();
			item.fromJSONObject(jo);
			this.Add(item);
		}
	}
	
}
